httpRequest = function (params) {
cc.log('发送请求', params.url);
var urlStr = 'http:
var methodStr = params.method || 'GET';
var async = false;
if (!!params.async) {
async = true;
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
if (!!params.callback) {
cc.log('收到数据', xhr);
params.callback(xhr);
}
}
};
if (!!params.responseType) {
xhr.responseType = params.responseType;
}
xhr.open(methodStr, urlStr, async);
xhr.send();
};
var Base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode: function encode(input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
while (i < input.length) {
chr1 = input[i++];
chr2 = input[i++];
chr3 = input[i++];
enc1 = chr1 >> 2;
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) + Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
}
return output;
}
};
httpRequest({
url: imgUrl,
async: true,
responseType: 'arraybuffer',
callback: function (data) {
var blob = new Uint8Array(data.response);
var img = new Image();
img.src = "data:image/png;base64," + Base64.encode(blob);
img.onload = function () {
var texture = new cc.Texture2D();
texture.generateMipmaps = false;
texture.initWithElement(img);
texture.handleLoadedTexture();
target.spriteFrame = new cc.SpriteFrame(texture);
}
}.bind(this)
});